package com.educhains.config;

import com.educhains.web.handler.EduchainsWebSocketHandler;
import com.educhains.web.interceptor.EduchainsHandleShakeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author YuRun
 * @since 2017年11月3日
 */
@Component
@EnableWebSocket
public class WebSocketConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {

    @Autowired
    EduchainsWebSocketHandler handler;

    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //if support WebSocket in frontend
        registry.addHandler(handler, "/websocket").addInterceptors(new EduchainsHandleShakeInterceptor());

        //if do not support WebSocket, test sockjs
        registry.addHandler(handler, "/sockjs/websocket").addInterceptors(new EduchainsHandleShakeInterceptor())
                .withSockJS();
    }

}
